package ru.isys.yoomee.ui;

import java.util.ArrayList;
import java.util.List;

import ru.isys.yoomee.R;
import ru.isys.yoomee.model.Venue;
import ru.isys.yoomee.util.timemap.TimemapItem;
import ru.isys.yoomee.util.timemap.TimemapItemizedOverlay;
import ru.isys.yoomee.util.timemap.TimemapMyLocationOverlay;
import android.location.Location;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

public class TimemapActivity extends MapActivity
{

    private static final String TAG = "MapViewCompassDemo";
    private ArrayList<Venue> nearbyVenues; 
    private MapView mMapView;
    private MyLocationOverlay mMyLocationOverlay;
    private static final int ONE_MINUTE = 1000 * 60 * 1;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timemap_layout);
        this.nearbyVenues = new ArrayList<Venue>();
        mMapView = (MapView) findViewById(R.id.mapView);
        mMyLocationOverlay = new TimemapMyLocationOverlay(this, mMapView);
        mMyLocationOverlay.runOnFirstFix(new Runnable()
        {
            public void run()
            {
                mMapView.getController().animateTo(mMyLocationOverlay.getMyLocation());
            }
        });

        

        TimemapItemizedOverlay markers = prepareNearbyVenuesOverlay();        
        mMapView.getOverlays().add(markers);        
        mMapView.getOverlays().add(mMyLocationOverlay);
        mMapView.setBuiltInZoomControls(true);
        mMapView.setSatellite(false);
        mMapView.getController().setZoom(16);
        mMapView.setClickable(true);
        mMapView.setEnabled(true);
    }

    private TimemapItemizedOverlay prepareNearbyVenuesOverlay()
    {
        List <Venue> nearbyVenues =mockVenues();
        
        TimemapItemizedOverlay markers = new TimemapItemizedOverlay(this,getResources().getDrawable(R.drawable.timemap_yoo_location));
        GeoPoint point = new GeoPoint(nearbyVenues.get(0).getLatitude(),nearbyVenues.get(0).getLongitude());
        markers.addOverlay(new TimemapItem(point, "", "", nearbyVenues.get(0)));
        point = new GeoPoint(nearbyVenues.get(1).getLatitude(),nearbyVenues.get(1).getLongitude());
        markers.addOverlay(new TimemapItem(point, "", "", nearbyVenues.get(1)));
        return markers;
    }

    private List<Venue> mockVenues()
    {
        List<Venue> venues = new ArrayList<Venue>();
        Venue venue = new Venue("I-sys");
        venue.setLatitude((int)(53.199359*1E6));
        venue.setLongitude((int)(50.189559*1E6));
        venues.add(venue);
        
        venue = new Venue("ТЦ Аврора");
        venue.setLatitude((int)(53.191289*1E6));
        venue.setLongitude((int)(50.189136*1E6));
        
        venues.add(venue);
        return venues;
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        mMyLocationOverlay.enableMyLocation();
    }

    @Override
    protected void onPause()
    {
        mMyLocationOverlay.disableMyLocation();
        super.onPause();
    }

    @Override
    protected void onStop()
    {
        mMyLocationOverlay.disableMyLocation();
        super.onStop();
    }

    @Override
    protected boolean isRouteDisplayed()
    {
        return false;
    }

    /**
     * Determines whether one Location reading is better than the current Location fix
     * 
     * @param location
     *            The new Location that you want to evaluate
     * @param currentBestLocation
     *            The current Location fix, to which you want to compare the new one
     */
    protected boolean isBetterLocation(Location location, Location currentBestLocation)
    {
        if (currentBestLocation == null)
        {
            // A new location is always better than no location
            return true;
        }

        // Check whether the new location fix is newer or older
        long timeDelta = location.getTime() - currentBestLocation.getTime();
        boolean isSignificantlyNewer = timeDelta > ONE_MINUTE;
        boolean isSignificantlyOlder = timeDelta < -ONE_MINUTE;
        boolean isNewer = timeDelta > 0;

        // If it's been more than two minutes since the current location, use the new location
        // because the user has likely moved
        if (isSignificantlyNewer)
        {
            return true;
            // If the new location is more than two minutes older, it must be worse
        }
        else if (isSignificantlyOlder)
        {
            return false;
        }

        // Check whether the new location fix is more or less accurate
        int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
        boolean isLessAccurate = accuracyDelta > 0;
        boolean isMoreAccurate = accuracyDelta < 0;
        boolean isSignificantlyLessAccurate = accuracyDelta > 200;

        // Check if the old and new location are from the same provider
        boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider());

        // Determine location quality using a combination of timeliness and accuracy
        if (isMoreAccurate)
        {
            return true;
        }
        else if (isNewer && !isLessAccurate)
        {
            return true;
        }
        else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider)
        {
            return true;
        }
        return false;
    }

    /** Checks whether two providers are the same */
    private boolean isSameProvider(String provider1, String provider2)
    {
        if (provider1 == null)
        {
            return provider2 == null;
        }
        return provider1.equals(provider2);
    }

}
